package com.hxqh.bo.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hxqh.bo.bean.FpsUserTradeLog;
import com.hxqh.bo.dao.FpsUserTradeLogDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * FpsUserTradeLog entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hxqh.bo.bean.FpsUserTradeLog
 * @author MyEclipse Persistence Tools
 */

public class FpsUserTradeLogDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(FpsUserTradeLogDAO.class);
	// property constants
	public static final String USER_ID = "userId";
	public static final String POLICY_ID = "policyId";
	public static final String TRADE_PRICE = "tradePrice";
	public static final String TRADE_NUM = "tradeNum";
	public static final String SELL = "sell";
	public static final String FUTURES_CODE = "futuresCode";
	public static final String AUTOMATIC = "automatic";
	public static final String DIRECTION = "direction";
	public static final String ACTUAL_TRADE_NUM = "actualTradeNum";
	public static final String FINISH = "finish";
	public static final String POLICY_ORDER_NO = "policyOrderNo";

	protected void initDao() {
		// do nothing
	}
	
	public void truncateTable() {
		getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql  = "truncate table fps_user_trade_log";
				SQLQuery q = arg0.createSQLQuery(sql);
				q.executeUpdate();
				return null;
			}
			
		});
	}
	
	public void save(FpsUserTradeLog transientInstance) {
		log.debug("saving FpsUserTradeLog instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(FpsUserTradeLog transientInstance) {
		log.debug("updateing FpsUserTradeLog instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	public void delete(FpsUserTradeLog persistentInstance) {
		log.debug("deleting FpsUserTradeLog instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public FpsUserTradeLog findById(java.lang.Long id) {
		log.debug("getting FpsUserTradeLog instance with id: " + id);
		try {
			FpsUserTradeLog instance = (FpsUserTradeLog) getHibernateTemplate()
					.get("com.hxqh.bo.bean.FpsUserTradeLog", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(FpsUserTradeLog instance) {
		log.debug("finding FpsUserTradeLog instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FpsUserTradeLog instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from FpsUserTradeLog as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByPolicyId(Object policyId) {
		return findByProperty(POLICY_ID, policyId);
	}

	public List findByTradePrice(Object tradePrice) {
		return findByProperty(TRADE_PRICE, tradePrice);
	}

	public List findByTradeNum(Object tradeNum) {
		return findByProperty(TRADE_NUM, tradeNum);
	}

	public List findBySell(Object sell) {
		return findByProperty(SELL, sell);
	}

	public List findByFuturesCode(Object futuresCode) {
		return findByProperty(FUTURES_CODE, futuresCode);
	}

	public List findByAutomatic(Object automatic) {
		return findByProperty(AUTOMATIC, automatic);
	}

	public List findByDirection(Object direction) {
		return findByProperty(DIRECTION, direction);
	}

	public List findByActualTradeNum(Object actualTradeNum) {
		return findByProperty(ACTUAL_TRADE_NUM, actualTradeNum);
	}

	public List findByFinish(Object finish) {
		return findByProperty(FINISH, finish);
	}

	public List findByPolicyOrderNo(Object policyOrderNo) {
		return findByProperty(POLICY_ORDER_NO, policyOrderNo);
	}

	public List findAll() {
		log.debug("finding all FpsUserTradeLog instances");
		try {
			String queryString = "from FpsUserTradeLog";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public FpsUserTradeLog merge(FpsUserTradeLog detachedInstance) {
		log.debug("merging FpsUserTradeLog instance");
		try {
			FpsUserTradeLog result = (FpsUserTradeLog) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FpsUserTradeLog instance) {
		log.debug("attaching dirty FpsUserTradeLog instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(FpsUserTradeLog instance) {
		log.debug("attaching clean FpsUserTradeLog instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static FpsUserTradeLogDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (FpsUserTradeLogDAO) ctx.getBean("FpsUserTradeLogDAO");
	}
}